﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.IO;

namespace SportManager
{
    /// <summary>
    /// Helper class, that can find items by GUID in list of data
    /// </summary>
    public class ListItemsFinder
    {
        public static TDataType FindItemByGuid<TDataType>(List<TDataType> data, Guid guid)
            where TDataType : DataHolder
        {
            int index = GetItemIndex(data, guid);
            if (index >= 0)
            {
                return data[index];
            }

            throw new Exception("Item not found!");
        }

        public static int GetItemIndex<TDataType>(List<TDataType> data, Guid guid)
            where TDataType : DataHolder
        {
            return data.FindIndex(item => item.Id == guid);
        }
    }

    /// <summary>
    /// Helper class for merging two exercise lists to one list.
    /// </summary>
    public class ExerciseMerger
    {
        public static List<Exercise> MergeLists(List<ExerciseInfo> selectedItems, List<Exercise> existingItems)
        {
            List<Exercise> exercises = new List<Exercise>();
            foreach (ExerciseInfo info in selectedItems)
            {
                bool isFound = false;
                foreach (Exercise ex in existingItems)
                {
                    if (ex.Id == info.Id)
                    {
                        isFound = true;
                        exercises.Add(ex);
                        break;
                    }
                }

                if (isFound == false)
                {
                    exercises.Add(new Exercise(info.Id, new List<Approach>(), new List<Approach>()));
                }
            }

            return exercises;
        }        
    }

    /// <summary>
    /// Class for strings encryption and decryption.
    /// </summary>
    public class Encryptor
    {
        public static string Encrypt(string originalString)
        {
            if (String.IsNullOrEmpty(originalString))
            {
                return "";
            }

            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);
            StreamWriter writer = new StreamWriter(cryptoStream);
            writer.Write(originalString);
            writer.Flush();
            cryptoStream.FlushFinalBlock();
            writer.Flush();
            return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
        }

        public static string Decrypt(string cryptedString)
        {
            if (String.IsNullOrEmpty(cryptedString))
            {
                return "";
            }

            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(cryptedString));
            CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
            StreamReader reader = new StreamReader(cryptoStream);
            return reader.ReadToEnd();
        }

        private static byte[] bytes = System.Text.ASCIIEncoding.ASCII.GetBytes("P@$$w0rD");
    }
}